<!DOCTYPE html>
<html xmlns:border="http://www.w3.org/1999/xhtml" xmlns: xmlns:display="http://www.w3.org/1999/xhtml" xmlns:
      xmlns:width="http://www.w3.org/1999/xhtml">
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <title>传智健康</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>

    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <style type="text/css">
        body, html {
            width: 100%;
            height: 100%;
            margin: 0;
            font-family: "微软雅黑";
            font-size: 14px;
        }

        #l-map {
            height: 500px;
            width: 100%;
        }

        #map2 {
            height: 500px;
            width: 100%;
        }

        #r-result {
            width: 100%;
        }

        #r-result2 {
            width: 100%;
        }


    </style>

    <script type="text/javascript"
            src="http://api.map.baidu.com/api?v=2.0&ak=TwBb7cqto5AwQUyfneG6swdWMnSCvqy3"></script>

    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }

        #allmap {
            height: 450px;
            width: 100%;
            overflow: hidden;
            font-family: "微软雅黑";
            border: 1px solid green;
        }

        /*百度地图搜索框定位*/
        .tangram-suggestion-main {
            z-index: 999999;
        }

        /*设置弹出框样式*/
        .el-dialog__body {
            padding: 0px 20px;
            color: #606266;
            font-size: 14px;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>工作台
            <small>公司分布图</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">工作台</el-breadcrumb-item>
            <el-breadcrumb-item>公司分布图</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <template>
            <!--分栏-->
            <el-tabs v-model="activeName" @tab-click="handleClick">
                <!--分布图-->
                <el-tab-pane label="分布图" name="first" @on-change="flush">
                    <iframe name="flush" class="el-main" src="../pages/map.html" width="100%" height="580px"
                            frameborder="0" id="flush"></iframe>
                </el-tab-pane>

                <!--详细信息-->
                <el-tab-pane label="详细信息" name="second">
                    <div class="filter-container">
                        <el-input placeholder="公司地址" v-model="pagination.queryString" style="width: 200px;"
                                  class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                        <el-button @click="query()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="address" label="公司地址" align="center"></el-table-column>

                        <el-table-column prop="longitude" label="所在经度" align="center"></el-table-column>
                        <el-table-column prop="latitude" label="所在纬度" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <!--分页组件-->
                    <div class="pagination-container">
                        <el-pagination
                                class="pagiantion"
                                @current-change="handleCurrentChange"
                                :current-page="pagination.currentPage"
                                :page-size="pagination.pageSize"
                                layout="total, prev, pager, next, jumper"
                                :total="pagination.total">
                        </el-pagination>
                    </div>

                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增地址" :visible.sync="dialogFormVisible">
                            <div>
                                <div id="r-result">
                                    <input type="text" id="suggestId" size="50" style="width: 200px;height: 30px;"/>
                                    <!--<input type="hidden" id="hide">-->
                                    <el-button type="primary" @click="addAddress()">确认添加</el-button>
                                </div>
                                <div id="l-map"></div>
                                <div id="searchResultPanel"
                                     style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
                            </div>
                        </el-dialog>
                    </div>
                </el-tab-pane>

            </el-tabs>

        </template>
    </div>

    </el-tab-pane>
    </el-tabs>
    </template>
</div>
</body>

<script>

    //地图对象
    var map;

    //地图坐标
    var pp;

    let vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 5,
                total: 1,
                queryString: null,
            },

            dataList: [],//列表数据
            dialogFormVisible: false,//控制添加窗口显示/隐藏
        },

        created() {
            this.findPage();
        },

        methods: {
            //切换列表时刷新引入的地图
            handleClick() {
                document.getElementById('flush').contentWindow.location.reload(true);
            },

            //解决查询分页查询bug
            query() {
                this.pagination.currentPage = 1;
                this.findPage();
            },

            findPage() {
                //分页查询需要的参数
                var parm = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                //请求后台
                axios.post("/map/findPage.do", parm).then((res) => {
                    this.dataList = res.data.rows;
                    this.pagination.total = res.data.total;
                });
            },

            //切换页码
            handleCurrentChange(currentPage) {
                //重新设置最新页码
                this.pagination.currentPage = currentPage;
                //重新调用分页查询方法
                this.findPage();
            },

            //展示新建栏
            showDialogFormVisible() {
                this.dialogFormVisible = true;
            },

            //点击新增按钮
            handleCreate() {
                this.showDialogFormVisible();
                //让地图可以加载成功
                setTimeout(() => {
                    this.createMap()
                }, 10);
                // this.createMap();
            },

            //创建地图方法
            createMap() {
                // 百度地图API功能
                function G(id) {
                    return document.getElementById(id);
                }

                map = new BMap.Map("l-map");
                map.centerAndZoom("西安", 12);       // 初始化地图,设置城市和地图级别。
                map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放

                var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
                    {
                        "input": "suggestId"
                        , "location": map
                    });

                ac.addEventListener("onhighlight", function (e) {  //鼠标放在下拉列表上的事件
                    var str = "";
                    var _value = e.fromitem.value;
                    var value = "";
                    if (e.fromitem.index > -1) {
                        value = _value.province + _value.city + _value.district + _value.street + _value.business;
                    }
                    str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

                    value = "";
                    if (e.toitem.index > -1) {
                        _value = e.toitem.value;
                        value = _value.province + _value.city + _value.district + _value.street + _value.business;
                    }
                    str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                    G("searchResultPanel").innerHTML = str;
                });

                var myValue;
                ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
                    var _value = e.item.value;
                    myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
                    G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
                    setPlace();
                });

                function setPlace() {
                    map.clearOverlays();    //清除地图上所有覆盖物
                    function myFun() {
                        pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                        map.centerAndZoom(pp, 18);
                        map.addOverlay(new BMap.Marker(pp));    //添加标注
                    }

                    var local = new BMap.LocalSearch(map, { //智能搜索
                        onSearchComplete: myFun
                    });
                    local.search(myValue);
                }
            },

            //添加地址
            addAddress() {
                var keyword = $('#suggestId').val();
                var json = {address: keyword, longitude: pp.lng, latitude: pp.lat};
                axios.post("/map/addAddress.do", json).then((res) => {
                    if (res.data.flag) {
                        this.$message({
                            type: 'success',
                            message: res.data.message
                        });
                    } else {
                        this.$message.error(res.data.message);
                    }
                }).finally(
                    this.dialogFormVisible = false
                );
            },

            // 删除
            handleDelete(row) {
                this.$confirm("确定要删除当前数据吗", "提示", {
                    type: 'warning'
                }).then(() => {
                        axios.get("/map/delete.do?id=" + row.id).then((res) => {
                            if (res.data.flag) {
                                this.$message({
                                    type: 'success',
                                    message: res.data.message
                                });
                            } else {
                                this.$message.error(res.data.message);
                            }
                            this.query();
                        }).catch((r) => {
                                this.showMessage(r)
                            }
                        )
                    }
                ).catch(() => {
                    //点击取消按钮执行此代码
                    this.$message('操作已取消');
                })
            },

            //权限提示
            showMessage(r) {
                if (r == 'Error: Request failed with status code 403') {
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            }
        }
    });
</script>
</html>